<!DOCTYPE html>
<html lang="zh-cmn-Hans" prefix="og: http://ogp.me/ns#" class="han-init">
<head>
  <meta charset="utf-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <link rel="icon" href="http://upeng.github.io/favicon.ico">
  <title>upeng</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox-1.3.4.css">
  <!--在这里倒入jquery 方便处理部分页面的jquery-->
  <script src="https://cdn.staticfile.org/jquery/1.7/jquery.min.js" type="text/javascript" ></script>
</head>

<body>
	<header class="site-header navfixed-false">
  <div class="container">
      <h1><a href="/" title="upeng"><span class="octicon octicon-mark-github"></span> upeng</a></h1>
      <nav class="site-header-nav" role="navigation">
        
              
              <a href="/"  class=" site-header-nav-item hvr-underline-from-center" title="Home">Home</a>
        
              
              <a href="/categories/"  class=" site-header-nav-item hvr-underline-from-center" title="Categories">Categories</a>
        
              
              <a href="/bookmark/"  class=" site-header-nav-item hvr-underline-from-center" title="Bookmark">Bookmark</a>
        
              
              <a href="http://shareup.sinaapp.com"  class=" site-header-nav-item hvr-underline-from-center" title="Share">Share</a>
        
              
              <a href="/about/"  class=" site-header-nav-item hvr-underline-from-center" title="About">About</a>
        
      </nav>
  </div>
</header>

	
<section class="collection-head geopattern" data-pattern-id="静态缓存实践" >
    <div class="container">
        <div class="collection-title">
            <h1 class="collection-header">
                静态缓存实践
            </h1>
            
                <div class="collection-info">
                    <span class="meta-info">
                        <span class="octicon octicon-calendar"></span>
                        <time datetime="2016-11-11T16:19:47.000Z" itemprop="datePublished">2016-11-12</time>
                    </span>
                    
                        <span class="meta-info">
                            <span class="octicon octicon-file-directory"></span>
                            <a href='/categories/PHP/' title=''>PHP</a>
                        </span>
                    
                </div>
            
        </div>
    </div>
</section>
	<section class="container">
    <div class="columns">
        <!-- -->
        <div class="column three-fourths">
            <article class="article-content markdown-body">
                <h4 id="静态缓存"><a href="#静态缓存" class="headerlink" title="静态缓存"></a>静态缓存</h4><p>什么是静态缓存？简单理解就是保存在磁盘上的静态文件，将生成的数据放入静态文件中，一般是存储不经常变动内容的页面数据。<br><a id="more"></a><br><strong>思路：</strong></p>
<ul>
<li>创建缓存文件</li>
</ul>
<p>命名方式比如：以<code>key</code>作为文件名。先指定一个文件夹比如<code>files</code>用于缓存文件的文件夹，然后根据<code>key</code>和<code>path</code>创建具体缓存文件</p>
<ul>
<li>封装在一个方法中，根据参数做不同的操作（当然也可以封装三个方法）</li>
</ul>
<p><code>cacheData($key, $value = &#39;&#39;, $path = &#39;&#39;)</code></p>
<pre><code>//get cache
$value === &#39;&#39;

//delete cache
$value !== &#39;&#39; &amp;&amp; is_null($value)

//set cache
$value !== &#39;&#39; &amp;&amp; !is_null($value)
</code></pre><h4 id="用到的php文件处理函数"><a href="#用到的php文件处理函数" class="headerlink" title="用到的php文件处理函数"></a>用到的php文件处理函数</h4><pre><code>//获取缓存文件的文件（上级）目录
$dir = dirname($filename); 

//判断文件夹(目录)是否存在
is_dir($dir); 

//创建文件目录
mkdir($dir, 0777);

//判断文件是否存在
is_file($filename);

//删除文件
unlink($filename);
</code></pre><h4 id="封装静态缓存操作类"><a href="#封装静态缓存操作类" class="headerlink" title="封装静态缓存操作类"></a>封装静态缓存操作类</h4><pre><code>class Scache{
    const EXT = &#39;.txt&#39;;
    private $_dir;
    public function __construct(){
        $this-&gt;_dir = dirname() . &#39;/files/&#39;;
    }

    //一个函数，不同参数，产生【写缓存，读缓存，删缓存】
    public function cacheMeta($key, $value = &#39;&#39;, $path = &#39;&#39;){
        $filename = $this-&gt;_dir . $path . $key . self::EXT;
        if ($value !== &#39;&#39;){
            if (is_null($value)){
                return @unlink($filename);
            }

            $dir = dirname($filename);
            if (!is_dir($dir)){ //判断目录是否存在
                mkdir($dir, 0777);
            }

            //写入成功返回字节数，失败返回false
            return file_put_contents($filename, json_encode($value)); 
        }

        if(!is_file($filename)){
            return false;
        }
        return file_get_contents($filename);
    }

}
</code></pre><h4 id="操作"><a href="#操作" class="headerlink" title="操作"></a>操作</h4><pre><code>include_once &#39;scache.php&#39;;

$obj = new Scache;

//设置缓存
$key = &#39;learn_upeng_001&#39;;
$value = array(
    &#39;a&#39; =&gt; &#39;learn file cache function&#39;,
    &#39;b&#39; =&gt; array(
        &#39;c&#39; =&gt; &#39;for learn filename operation&#39;,
        &#39;d&#39; =&gt; &#39;learn oop at the same time&#39;,
    )
);
$obj-&gt;cacheMeta($key, $value);

//读取缓存
$obj-&gt;cacheMeta($key);

//删除缓存
$obj-&gt;cacheMeta($key, NULL);
</code></pre><blockquote>
<p>注：summary from imooc lesson</p>
</blockquote>

            </article>
            
                <div class="share">
                    <!--开启分享-->
<div class="share-component" data-disabled="google,twitter,facebook" data-description="静态缓存什么是静态缓存？简单理解就是保存在磁盘上的静态..."></div>

<script src="/js/share.min.js"></script>

                </div>    
            
            
                
<div class="comments">
	<div class="ds-thread" data-thread-key="static-cache" data-title="静态缓存实践" data-url="http://upeng.github.io/2016/11/12/static-cache/"></div>
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"upeng"};
		(function() {
			var ds = document.createElement('script');
			ds.type = 'text/javascript';ds.async = true;
			ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
			ds.charset = 'UTF-8';
			(document.getElementsByTagName('head')[0]
			 || document.getElementsByTagName('body')[0]).appendChild(ds);
		})();
	</script>
</div>

            
        </div>
        <div class="column one-fourth">
            
                
                


<h3>Post Directory</h3>

<div id="post-directory-module">
	<section class="post-directory">
		<p><strong class="toc-title">文章目录</strong></p>
		<ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#静态缓存"><span class="toc-text">静态缓存</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#用到的php文件处理函数"><span class="toc-text">用到的php文件处理函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#封装静态缓存操作类"><span class="toc-text">封装静态缓存操作类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#操作"><span class="toc-text">操作</span></a></li></ol>
	</section>
</div>
            
        </div>
    </div>
</section>

<footer class="container">
    <div class="site-footer" role="contentinfo">
        <div class="copyright left mobile-block">
                © 2016
                <span title="yupeng">yupeng</span>
                <a href="javascript:window.scrollTo(0,0)" class="right mobile-visible">TOP</a>
        </div>

        <ul class="site-footer-links right mobile-hidden">
            <li>
                <a href="javascript:window.scrollTo(0,0)" >TOP</a>
            </li>
        </ul>

        <a href="https://github.com/upeng" target="_blank" aria-label="view source code">
            <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
        </a>

        <ul class="site-footer-links mobile-hidden">
            
                  
                  <li>
                    <a href="/"  title="Home">Home</a>
                  </li>
            
                  
                  <li>
                    <a href="/categories/"  title="Categories">Categories</a>
                  </li>
            
                  
                  <li>
                    <a href="/bookmark/"  title="Bookmark">Bookmark</a>
                  </li>
            
                  
                  <li>
                    <a href="http://shareup.sinaapp.com"  title="Share">Share</a>
                  </li>
            
                  
                  <li>
                    <a href="/about/"  title="About">About</a>
                  </li>
            
            <li>
                <a href="/atom.xml">
                    <span class="octicon octicon-rss" style="color:orange;"></span>
                </a>
            </li>
        </ul>
    </div>
</footer>


		<script src="/js/geopattern.js"></script>

		
			<script src="/js/toc.js"></script>
		

		<script src="/js/highlight.pack.js"></script>
		<script src="/lib/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

		<script src="/js/index.js"></script>

		 <script src="/js/popular_repo.js"></script> 

	</body>
</html>